{% extends 'form.html' %}

{% block other_buttons %}
    <button id="send_code" class="btn btn-primary">发送验证码</button>
{% endblock %}

{% block script_extends %}
    <script type="text/javascript">
        $('#send_code').click(function (){
           var email = $('#id_email').val();
           if(email==""){
                $("#tip").text('* 邮箱不能为空');
                return false;
           }

           // 发送验证码
           $.ajax({
               url: "{% url 'send_verification_code' %}",
               type: "GET",
               data: {
                   'email': email,
                   'send_for': 'bind_email_code'
               },
               cache: false,
               success: function (data){
                   if(data['status']=='ERROR'){
                       alert(data['status']);
                   }
               }
           });

           // 把按钮变灰色
           $(this).addClass('disabled');
           $(this).attr('disabled', true);
           var time = 15;
           $(this).text(time + 's');
           var interval = setInterval(() => {
               if(time <= 0){
                   clearInterval(interval);
                   $(this).removeClass('disabled');
                   $(this).attr('disabled', false);
                   $(this).text('发送验证码');
                   return false;
               }

               time --;
               $(this).text(time + 's');
           }, 1000);
        });
    </script>
{% endblock %}